<template>
  <div class="main">
    <!-- 顶部轮播 -->
    <swiper :options="swiperOption">
      <swiper-slide v-for="(item, index) in bannerData" :key="index">
        <a target="_blank" :href="item.url">
          <img :src="item.image" alt="" srcset="" />
        </a>
      </swiper-slide>
    </swiper>
    <div class="center">
      <div class="lf">
        <!-- 左侧列表 -->
        <router-link
          class="item"
          :to="{ name: 'newsDetail', query: { id: item.id } }"
          v-for="(item, index) in newsData"
          :key="index"
        >
          <div class="img">
            <el-image lazy :src="item.image" alt="" />
          </div>
          <dl>
            <dt>{{ item.title }}</dt>
            <dd v-html="item.content"></dd>
          </dl>
        </router-link>
        <!-- 加载更多 -->
        <div class="more-btn" @click="loadMore">加载更多</div>
      </div>
      <div class="rt">
        <!-- 右侧部分 -->
        <router-link v-for="(item,index) in newestNewsData.slice(0,1)" :key="index" class="top" :to="{ name: 'newsDetail', query: { id: item.id } }">
          <el-image
            lazy
            :src="item.image"
            alt=""
          />
          <p>{{item.title}}</p>
        </router-link>
        <!-- 最新资讯 -->
        <div class="new">
          <div class="head">最新资讯</div>
          <div class="body">
            <div class="img-wrap">
              <router-link
                class="img"
                v-for="(item, index) in newestNewsData.slice(0, 2)"
                :key="index"
                :to="{ name: 'newsDetail', query: { id: item.id } }"
              >
                <el-image lazy :src="item.image" alt="" />
              </router-link>
            </div>
            <router-link
              :to="{ name: 'newsDetail', query: { id: item.id } }"
              v-for="(item, index) in newestNewsData.slice(2, 12)"
              :key="index"
            >
              <span>{{ index + 1 }}</span>
              {{ item.title }}
            </router-link>
          </div>
        </div>
        <!-- 推荐资讯 -->
        <div class="recommend">
          <div class="head">推荐资讯</div>
          <div class="body">
            <router-link
              :to="{ name: 'newsDetail', query: { id: item.id } }"
              v-for="(item, index) in recommendNewsData"
              :key="index"
              class="item"
            >
              <dl>
                <dt>{{item.title}}</dt>
                <dd v-html="item.content"></dd>
              </dl>
              <el-image
                lazy
                :src="item.image"
                alt=""
              />
            </router-link>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 顶部轮播图设置
      swiperOption: {
        autoplay: {
          disableOnInteraction: false, // 用户操作swiper之后，是否禁止autoplay
          delay: 5000, // 自动切换的时间间隔（单位ms）
        },
        // loop: true,
      },
      bannerData: [],
      newsData: [],
      newestNewsData: [],
      recommendNewsData:[],
      keyword: "",
      page: 1,
      limit: 10,
    };
  },
  props: ["_keyword"], //接收父组件传值
  // 监听父组件传的_keyword值的变化
  watch: {
    _keyword: function (_keyword) {
      this.keyword = _keyword;
      this.newsData = [];
      this.getNews(this.keyword, this.page, this.limit);
    },
  },
  created() {
    // 顶部轮播
    this.$http.getBanner().then((res) => {
      if (res.data.code === 0) {
        this.bannerData = res.data.data;
      } else {
        this.$message.error("获取失败，请重试！");
      }
    });
    this.getNews(this.keyword, this.page, this.limit);
    // 最新资讯
    this.$http.getNews().then((res) => {
      if (res.data.code === 0) {
        this.newestNewsData = res.data.data;
      } else {
        this.$message.error("获取失败，请重试！");
      }
    });
    // 推荐资讯
    this.$http.getNews({is_recommand:1}).then((res) => {
      if (res.data.code === 0) {
        this.recommendNewsData = res.data.data;
      } else {
        this.$message.error("获取失败，请重试！");
      }
    });
  },
  methods: {
    getNews(keyword, page, limit) {
      // 资讯列表
      let data = {
        keyword: keyword,
        page: page,
        limit: limit,
      };
      this.$http.getNews(data).then((res) => {
        if (res.data.code === 0) {
          if (res.data.data.length <= 0) {
            this.$message.error("没有更多数据！");
            this.page = 1;
            return;
          }
          this.newsData = this.newsData.concat(res.data.data);
        } else {
          this.$message.error("获取失败，请重试！");
        }
      });
    },
    // 加载更多
    loadMore() {
      this.getNews(++this.page, this.limit);
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep {
  .swiper-container {
    height: 400px;
    width: 100%;
    img {
      width: 100%;
      height: 100%;
    }
  }
}
.center {
  width: 1600px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  .lf {
    width: 1180px;
    margin-top: 30px;
    .item {
      display: flex;
      justify-content: space-between;
      padding: 40px 0;
      .img {
        width: 350px;
        height: 240px;
        .el-image {
          width: 100%;
          height: 100%;
        }
      }
      dl {
        width: 787px;
        dt {
          font-size: 20px;
          font-family: PingFang SC;
          font-weight: bold;
          line-height: 40px;
          color: #333333;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        dd {
          font-size: 16px;
          font-family: PingFang SC;
          font-weight: 400;
          line-height: 30px;
          color: #333333;
          margin-top: 20px;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 4;
          overflow: hidden;
        }
      }
    }
    .item:not(:first-child) {
      border-top: 1px solid #707070;
    }
    .item:hover {
      background: #e5e5e5;
    }
    .more-btn {
      width: 240px;
      height: 60px;
      background: #ffffff;
      border: 1px solid #707070;
      border-radius: 4px;
      font-size: 20px;
      font-family: PingFang SC;
      font-weight: 400;
      line-height: 60px;
      color: #333333;
      text-align: center;
      margin: 0 auto;
      margin-top: 70px;
      margin-bottom: 80px;
      cursor: pointer;
    }
  }
  .rt {
    width: 376px;
    margin-top: 70px;
    .top {
      display: block;
      width: 376px;
      height: 310px;
      position: relative;
      .el-image {
        width: 376px;
        height: 310px;
      }
      p {
        font-size: 20px;
        font-family: PingFang SC;
        font-weight: 400;
        width: 100%;
        height: 66px;
        line-height: 66px;
        background: linear-gradient(
          180deg,
          rgba(0, 0, 0, 0.4) 0%,
          #000000 100%
        );
        color: #ffffff;
        text-align: center;
        position: absolute;
        bottom: 0;
        left: 0;
      }
    }
    .new {
      .head {
        font-size: 20px;
        font-family: PingFang SC;
        font-weight: bold;
        line-height: 40px;
        color: #333333;
        margin-top: 40px;
      }
      .body {
        .img-wrap {
          display: flex;
          justify-content: space-between;
          margin: 20px 0;
          a {
            display: block;
            width: 180px;
            height: 180px;
            .el-image {
              width: 180px;
              height: 180px;
            }
          }
        }
        a {
          display: inline-block;
          font-size: 20px;
          font-family: PingFang SC;
          font-weight: 400;
          line-height: 40px;
          color: #333333;
          width: 376px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          span {
            display: inline-block;
            width: 30px;
            font-size: 24px;
            font-weight: bold;
            text-align: center;
          }
        }
        a:hover {
          text-decoration: underline;
        }
      }
    }
    .recommend {
      .head {
        font-size: 20px;
        font-family: PingFang SC;
        font-weight: bold;
        line-height: 40px;
        color: #333333;
        margin-top: 40px;
      }
      .body {
        margin-top: 20px;
        .item {
          display: flex;
          justify-content: space-between;
          padding: 20px 0;
          border-top: 1px solid #707070;
          dl {
            width: 184px;
            dt {
              margin-top: 7px;
              font-size: 20px;
              font-family: PingFang SC;
              font-weight: bold;
              line-height: 30px;
              color: #333333;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
            dd {
              margin-top: 10px;
              font-size: 16px;
              font-family: PingFang SC;
              font-weight: 400;
              line-height: 30px;
              color: #333333;
              display: -webkit-box;
              -webkit-box-orient: vertical;
              -webkit-line-clamp: 4;
              overflow: hidden;
            }
          }
          .el-image {
            width: 180px;
            height: 180px;
          }
        }
        .item:hover dd {
          text-decoration: underline;
        }
      }
    }
  }
}
</style>